<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org/">
<head>
    <meta charset="utf-8">
    <title>子账户管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" th:href="@{lib/layui-v2.6.3/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{css/public.css}" media="all">
    <style type="text/css">
        /* 设置下拉框的高度与表格单元相同 */
        td .layui-form-select {
            margin-top: -10px;
            margin-left: -15px;
            margin-right: -15px;
        }
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">子账户名</label>
                            <div class="layui-input-inline">
                                <input type="text" name="username" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">真实姓名</label>
                            <div class="layui-input-inline">
                                <input type="text" name="name" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary" lay-submit
                                    lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索
                            </button>
                            <button id="add_user_btn" type="button" class="layui-btn">
                                <i class="layui-icon"></i>
                                添加子账户
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">修改</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="shouquan">授权</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>

        </script>

        <script type="text/html" id="role_select">
            <select lay-filter="role" name="{{d.roleId}}">
                <option value="">请选择</option>
                <option th:each="role:${roleList}" th:value="${role.getId()}" th:text="${role.getRoleName()}"></option>
            </select>
        </script>

    </div>
</div>
<script th:src="@{lib/layui-v2.6.3/layui.js}" charset="utf-8"></script>

<script>

    layui.use(['form', 'table'], function () {

        var $ = layui.jquery, form = layui.form, table = layui.table;

        table.render({
            elem: '#currentTableId',
            url: '/sub_user_all',
            // toolbar: '#toolbarDemo',
            // defaultToolbar: ['filter', 'exports', 'print', {
            //     title: '提示',
            //     layEvent: 'LAYTABLE_TIPS',
            //     icon: 'layui-icon-tips'
            // }],
            cols: [
                [
                    {type: "numbers", width: 50},
                    {field: 'userName', title: '子账户名', edit: 'text'},
                    {field: 'userPwd', title: '密码'},
                    {field: 'name', title: '真实姓名', edit: 'text'},
                    /*{
                        field: "roleName", title: "角色", templet: function (d) {
                            if (d.roleName == "" || d.roleName == null) {
                                return "<span class=\"layui-badge-dot \"></span><span style='color: #FF5722;margin-left: 5px;'>暂无</span>";
                            } else {
                                if (d.roleStatus == 0) {
                                    return "<span class=\"layui-badge-dot layui-bg-orange\"></span><span style='color:#ffcc39;margin-left: 5px;'>" + d.roleName + "</span>"
                                } else {
                                    return "<span class=\"layui-badge-dot layui-bg-green\" style='margin-right: 5px;'></span>" + d.roleName;
                                }
                            }
                        }
                    },*/
                    {title: '操作', toolbar: '#currentTableBar', align: "center"}
                ]
            ]
            , done: function (res, curr, count) {
                //数据渲染完的回调。
                //由于layui 设置了超出隐藏，所以这里改变下，以兼容操作按钮的下拉菜单
                $(".layui-table-body, .layui-table-box, .layui-table-cell").css('overflow', 'visible');
            },

            limits: [10, 15, 20, 25, 50, 100],
            limit: 10,
            page: true,
            skin: 'line'
        });

        //监听单元格编辑
        table.on('edit(currentTableFilter)', function (obj) {
            var value = obj.value //得到修改后的值
                , data = obj.data //得到所在行所有键值 当前选中行的对象
                , field = obj.field; //得到字段
            // layer.alert('[ID: '+ data.id +'] ' + field + ' 字段更改为：'+ value);
            $.ajax({
                url: "/update_user",
                type: "post",
                data: {field: field, value: value, id: data.id},
                success: function (res) {
                    if (res.code == "0000") {
                        layer.msg("修改成功!");
                    } else {
                        layer.msg(res.msg, function () {
                            window.location.reload();
                        })
                    }
                }
            })

        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            var result = JSON.stringify(data.field);
            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                },
                where: {
                    searchParams: result
                }
            }, 'data');

            return false;
        });

        $("#add_user_btn").click(function () {
            var index = layer.open({
                title: '添加子账户',
                type: 2,
                shade: 0.2,
                maxmin: true,
                shadeClose: true,
                area: ['50%', '70%'],
                content: '/add_child_page',
            });
            $(window).on("resize", function () {
                layer.full(index);
            });
        })

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;//获取单签选中行的数据
            if (obj.event === 'edit') {

                var index = layer.open({
                    title: '修改',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['50%', '70%'],
                    content: '/update_child_page?id=' + data.id,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            }else if (obj.event === 'shouquan') {
                var index = layer.open({
                    title: '子账户授权',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['50%', '70%'],
                    content: '/goChildAuth?userId='+data.id,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            }
            else if (obj.event === 'delete') {
                layer.confirm('真的要删除吗？', function (index) {

                    $.ajax({
                        url: "del_user",
                        type: "get",
                        data: {id: data.id},
                        success: function (res) {
                            if (res.code == "0000") {
                                layer.msg(res.msg);
                                obj.del();
                            } else {
                                layer.msg(res.msg);
                            }
                        }
                    })

                    layer.close(index);
                });
            }
        });

    });
</script>

</body>
</html>